<template>
  <div class="login-wrap">
    <div class="ms-title">美发沙龙管理系统</div>
    <div class="ms-login">
      <el-form :model="loginParamsModel" :rules="loginParamsRule" ref="loginParamsModel" label-width="0px" class="demo-ruleForm">
        <div v-if="errorInfo">
          <span>{{errInfo}}</span>
        </div>
        <el-form-item prop="name">
          <el-input v-model="loginParamsModel.name" placeholder="账号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" placeholder="密码" v-model="loginParamsModel.password" @keyup.enter.native="submitForm('loginParamsModel')"></el-input>
        </el-form-item>
        <el-form-item prop="validate">
          <el-input v-model="loginParamsModel.validate" class="validate-code" placeholder="验证码"></el-input>
          <div class="code" @click="refreshCode">
            <identify :identifyCode="identifyCode"></identify>
          </div>
        </el-form-item>
        <div class="login-btn">
          <el-button type="primary" @click="login">登录</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Action } from 'vuex-class'
import { Tree } from 'element-ui';
import Identify from '@/components/Identify.vue'
import { LoginService } from '~/service/service-manage/login.service';
import { Dependencies } from '~/core/decorator';



@Component({
  components: {
    Identify
  }
})
export default class Login extends Vue {

  @Dependencies(LoginService)
  private loginService: LoginService;
  @Action("updateUserLoginData")
  private updateUserLoginData;

  /**
   * 初始验证
   */
  private identifyCodes = '1234567890';
  private identifyCode = '';
  private errorInfo = false;

  /**
   * 登陆参数验证
   */
  private loginParamsRule: any = {
    name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    validate: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
  };

  /**
   * 登陆参数验证
   */
  private loginParamsModel: any = {
    name: '',
    password: '',
    validate: ''
  };

  mounted() {
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  };

  /**
   * 验证码校验
   */
  private validateCode() {
    let validate = this.loginParamsModel.validate.trim()
    if (validate === '') {
      this.$message({ message: '验证码为空', center: true, type: 'error' })
      return false
    }
    if (validate !== this.identifyCode) {
      this.$message({ message: '验证码错误', center: true, type: 'error' })
      this.refreshCode()
      return false
    }
    return true
  };
  /**
   * 登陆
   */
  private login() {
    if (!this.validateCode()) {
      return
    }
    let userName = this.loginParamsModel.name
    let password = this.loginParamsModel.password
    this.loginService.login({ userName, password })
      .subscribe(({ token, userInfo }) => {
        // 更新store数据
        this.updateUserLoginData({ token, userInfo })
        this.$router.push("/home-page/my-page");
      },
        ({ msg }) => {
          this.$message.error(msg);
        }
      )
  };

  private randomNum(min, max) {
    return Math.floor(Math.random() * (max - min) + min)
  };

  private refreshCode() {
    this.identifyCode = ''
    this.makeCode(this.identifyCodes, 4)
  };

  private makeCode(o, l) {
    for (let i = 0; i < l; i++) {
      this.identifyCode += this.identifyCodes[this.randomNum(0, this.identifyCodes.length)]
    }
  };
}

</script>

<style lang="less" scoped>
.login-wrap {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: #324157;
}

.ms-title {
  position: absolute;
  top: 50%;
  width: 100%;
  margin-top: -200px;
  text-align: center;
  font-size: 30px;
  color: #fff;
}

.ms-login {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 300px;
  height: 240px;
  margin: -150px 0 0 -190px;
  padding: 40px;
  border-radius: 5px;
  color: #fff;
}

.ms-login span {
  color: red;
}

.login-btn {
  text-align: center;
}

.login-btn button {
  width: 100%;
  height: 36px;
}

.code {
  width: 112px;
  height: 35px;
  border: 1px solid #ccc;
  float: right;
  border-radius: 2px;
}

.validate-code {
  width: 136px;
  float: left;
}
</style>
